#ifndef SRC_NET_EV_POLL_H
#define SRC_NET_EV_POLL_H

#include "base/base.h"

#define EV_READ 1
#define EV_WRITE 2

namespace net {
struct Event {
    int fd;
    short event;
    void *arg;
    void (*cb)(int fd, void* arg, int event);
};

class Poller {
  public:
    virtual ~Poller() {
      Stop();
    }

    virtual bool Init() = 0;
    virtual void Stop() {
      if (!exit_) {
        exit_ = true;
        stop_cond_.Wait();
      }
    }

    virtual bool Add(Event* ev, int flag) = 0;
    virtual void Del(Event* ev, int flag) = 0;
    virtual bool Mod(Event* ev, int flag) = 0;

    // change exit_ false
    virtual void Loop() = 0;
    void LoopInAnothreadThread() {
      loop_thread_.reset(
          new StopableThread(new MethordClosure0<Poller>(this, &Poller::Loop)));
      loop_thread_->Start();
    }

  protected:
    Poller()
        : exit_(true) {
    }

    bool exit_;
    scoped_ptr<StopableThread> loop_thread_;
    SyncEvent stop_cond_;

  private:

    DISALLOW_COPY_AND_ASSIGN(Poller);
};

Poller* CreatePoller();

}
#endif  // SRC_NET_EV_POLL_H
